package cn.nexd.app.collector.adapter;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.nexd.app.collector.R;
import cn.nexd.app.collector.presenter.db.TableContract;
import cn.nexd.app.collector.widget.OnRecyclerViewItemClickListener;


/**
 * Created by xun on 2016/8/10.
 */
public class CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder> {

    private List<Map<String, Object>> cities;
    private OnRecyclerViewItemClickListener itemClickListener;
    private int selection = 0;

    private static final String TAG = "CityAdapter";

    public CityAdapter() {
        this.cities = new ArrayList<>();
    }

    public void setCities(List<Map<String, Object>> cities) {
        this.cities.clear();
        this.cities.addAll(cities);
        notifyDataSetChanged();
    }

    public void setItemClickListener(OnRecyclerViewItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_item_city, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.tvCity.setText(cities.get(position).get(TableContract.City.COLUMN_CITY_NAME).toString());

        if (selection == position) {
            holder.tvCity.setTextColor(Color.parseColor("#44BF1A"));
            holder.tvStatus.setBackgroundColor(Color.parseColor("#44bf1a"));
            holder.itemView.setBackgroundColor(Color.TRANSPARENT);
        } else {
            holder.tvCity.setTextColor(Color.parseColor("#000000"));
            holder.tvStatus.setBackgroundColor(Color.TRANSPARENT);
            holder.itemView.setBackgroundColor(Color.LTGRAY);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.setSelection(position);
                itemClickListener.onClick(position, holder);
                notifyDataSetChanged();
            }
        });

    }
    public void resetSelection(){
        selection=0;
    }


    @Override
    public int getItemCount() {
        return cities.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvStatus;
        TextView tvCity;

        public ViewHolder(View itemView) {
            super(itemView);
            tvStatus = (TextView) itemView.findViewById(R.id.item_tv_bg);
            tvCity = (TextView) itemView.findViewById(R.id.item_tv_city);
        }

        public void setSelection(int position) {
            selection = position;
        }

        public String getCityName() {
            return tvCity.getText().toString();
        }
    }
}
